package aom.ju.ss.view.shua

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import aom.ju.ss.R
import aom.ju.ss.databinding.*
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.mode.PublicMode
import aom.ju.ss.mode.ShopVideoList
import aom.ju.ss.mode.ShopVideoListData
import aom.ju.ss.mode.VideoListData
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.ListHolder
import aom.ju.ss.view.base.PAdapter
import aom.ju.ss.view.base.jzvd.JYVideoPlayer
import aom.ju.ss.view.base.jzvd.JzvdStd
import aom.ju.ss.view.dialog.CommentDialog
import aom.ju.ss.view.dialog.NavigationPopup
import aom.ju.ss.view.dialog.SharePromotionDialogLp
import aom.ju.ss.view.main.MainActivity
import com.bumptech.glide.Glide
import com.google.gson.Gson

/**
 * 店铺详情-> 视频详情
 */
class ShopVideoActivity : BaseActivity() {
    private lateinit var binding: ShopVideoListActivityBinding
    private lateinit var pAdapter: PAdapter
    private var videoList = arrayListOf<ShopVideoListData>()
    lateinit var layoutManager: LinearLayoutManager
    private lateinit var snapHelper: PagerSnapHelper
    private var videoPlayer: JYVideoPlayer? = null
    override fun init() {
        binding = viewBinding as ShopVideoListActivityBinding

        layoutManager = LinearLayoutManager(activity)
        val mode = Gson().fromJson(intent.getStringExtra("data"), ShopVideoList::class.java)
        mode.data?.let {
            videoList.addAll(it)
        }
        pAdapter = PAdapter(
            videoList, R.layout.shop_video_act_item
        ) { bb, position ->
            val ib = bb as ShopVideoActItemBinding
            val info = videoList[position]
            ib.info = info
            if (position == 0 && videoPlayer == null) {
                videoPlayer = ib.jyVideo
            }
            val url = videoList[position].video_url
            ib.jyVideo.setUp(url, "", JzvdStd.STATE_PREPARING)
            if (position == 0) {
                ib.jyVideo.startVideo()
            }
            Glide.with(this)
                .load(videoList[position].video_url + "?vframe/jpg/offset/0/")
                .into(ib.jyVideo.thumbImageView)

            ib.jyVideo.mRetryBtn.setOnClickListener {
                ib.jyVideo.setUp(url, "", JzvdStd.STATE_PREPARING)
                ib.jyVideo.startVideo()
            }
            ib.dianzan.setOnClickListener {
                OKHttpUtils(activity, false)
                    .SetApiUrl(LP_API.video_like)
                    .SetKey("id")
                    .SetValue(info.id.toString())
                    .POST(object : MyHttpCallBack {
                        override fun ok(jsonString: String?, httpTY: String?) {
                            val mode = Gson().fromJson(jsonString, PublicMode::class.java)
                            if (mode.msg == "点赞成功") {
                                "点赞成功".show()
                                info.is_like = 1
                                info.like_num++
                            } else {
                                info.is_like = 0
                                info.like_num--
                            }
                            videoList[position] = info
                            ib.info = info
                        }

                        override fun error(e: String?, jsonString: String?, httpTY: String?) {
                            e?.show
                        }


                    })
            }
            ib.name.setOnClickListener {

            }
            //分享
            ib.fx.setOnClickListener {
                SharePromotionDialogLp(info.id, activity!!) {
                    info.share_num++
                    videoList[position] = info
                    ib.info = info
                }

            }

            //评论
            ib.pl.setOnClickListener {
                CommentDialog(
                    activity!!,
                    info.id.toString(),
                    object : CommentDialog.CommentCallback {
                        override fun commentCallback() {
                            videoList[position].comment_num++
                            ib.info = videoList[position]
                        }
                    })
            }

            ib.boom.setOnClickListener { ib.pl.performClick() }
            // 导航
            ib.addTc.setOnClickListener {
                if (info.shop.lat != null && info.shop.lat!!.length > 5 && info.shop.lng != null && info.shop.lng!!.length > 5) {
                    NavigationPopup(activity, info.shop.lat, info.shop.lng)
                }
            }
            ib.name.setOnClickListener { ib.addTc.performClick() }
            ib.juli.setOnClickListener { ib.addTc.performClick() }
        }

        snapHelper = PagerSnapHelper()
        binding.list.mRecycler.layoutManager = layoutManager
        binding.list.mRecycler.adapter = pAdapter
        snapHelper.attachToRecyclerView(binding.list.mRecycler)
        binding.list.mRecycler.addOnScrollListener(
            object : RecyclerView.OnScrollListener() {

                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        val view = snapHelper.findSnapView(layoutManager) ?: return
                        val childViewHolder = binding.list.mRecycler.getChildViewHolder(view)
                        val vh = childViewHolder as ListHolder
                        val ib = vh.binding as ShopVideoActItemBinding

                        videoPlayer = ib.jyVideo
                        ib.jyVideo.startVideo()

                        val view1 = snapHelper.findSnapView(layoutManager) ?: return
                        val position = binding.list.mRecycler.getChildAdapterPosition(view1)


//                        if (position == videoList.size - 1 && !isLoading) {
//                            isLoading = true
//                            page++
//                            getData()
//                        }

                    }
                }
            })
        binding.title.r_img_two.setOnClickListener {
            "尽请期待".show
        }
        binding.list.small.isEnabled = false

    }

    override val lay: Int = R.layout.shop_video_list_activity
}